package com.fingard.dsp.bank.directbank.cfcc01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * @author Parker on 2019-08-08.
 * @version 1.0
 */
public class CFCC01Balance extends CFCC01Base implements IBalance {
    //单个余额查询和批量余额查询，统一采用流处理方式
    //1、循环调用Req7506BalQry.readRecord()读取一个请求明细，然后写入发银行的XML对象
    //2、发银行
    //3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
    //   然后调用RetAts7506BalQry.writeRetDetail()，写入返回ATS的明细
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            CFCC01Header bankHeader = new CFCC01Header();
            bankHeader.transCode = "todayBalQuery";
            while (p_req7506.readRecord()) {
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("REQUEST");
                xmlWriter.writeElementString("TRANTYPE", "003");
                xmlWriter.writeElementString("UNIQUECODE", getQryTransID());//唯一码
                xmlWriter.writeElementString("ACCTNO", p_req7506.readedDetail.actNum);
                xmlWriter.writeElementString("CUSTID", bankFront.org);//客户号
                xmlWriter.writeElementString("CURRENCY", convertCurToBank(p_req7506.readedDetail.actCur));
                //xmlWriter.writeElementString("branchkind", "");//开户行行别名称
                xmlWriter.writeElementString("ACCTFLAG", "0");//账号标志 0：财务公司结算账号1：银行账号
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());
                if (tmpRetBkStr[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/TRANSACTION/HEADER/REP_CODE");
                        String returnMsg = tmpBkReader.getFirstText("/TRANSACTION/HEADER/REP_DESC");
                        if (returnCode != null && returnCode.equals("0000")) {
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = returnMsg;
                            tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
                            tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
                            tmpRetDetail.actArea = p_req7506.readedDetail.actArea;

                            tmpRetDetail.currBal = tmpBkReader.getFirstText("/TRANSACTION/RESPONSE/LIST/BALANCE");//余额
                            tmpRetDetail.availBal = tmpBkReader.getFirstText("/TRANSACTION/RESPONSE/LIST/BALANCE");//可用余额
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        } else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(returnMsg);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpRetBkStr[1]);
                }
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            CFCC01Header bankHeader = new CFCC01Header();
            bankHeader.transCode = "historyBalQuery";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("REQUEST");
            xmlWriter.writeElementString("TRANTYPE", "003");
            xmlWriter.writeElementString("UNIQUECODE", getQryTransID());//唯一码
            xmlWriter.writeElementString("ACCTNO", p_req7505.actNum);
            xmlWriter.writeElementString("CUSTID", bankFront.org);//客户号
            xmlWriter.writeElementString("CURRENCY", convertCurToBank(p_req7505.actCur));
            //xmlWriter.writeElementString("branchkind", "");//开户行行别名称
            xmlWriter.writeElementString("BEGINDATE", p_req7505.dateFrom);
            xmlWriter.writeElementString("ENDDATE", p_req7505.dateTo);
            xmlWriter.writeElementString("ACCTFLAG", "0");//账号标志 0：财务公司结算账号1：银行账号
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());
            if (tmpRetBkStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/TRANSACTION/HEADER/REP_CODE");
                    String returnMsg = tmpBkReader.getFirstText("/TRANSACTION/HEADER/REP_DESC");
                    if (returnCode != null && returnCode.equals("0000")) {
                        while (tmpBkReader.read("/TRANSACTION/RESPONSE/LIST")) {
                            RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                            tmpRetDetail.balDate = tmpBkReader.readedXElement.getSingleText("TRANDATE");
                            tmpRetDetail.actBal = tmpBkReader.readedXElement.getSingleText("BALANCE");
                            tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("BALANCE");
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(returnMsg);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpRetBkStr[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }
}
